/**
 *
 * @author 朱雀
 */
Ext.define('SvgEditor.graph.Path', {
	extend: 'SvgEditor.graph.Model',
	init: function(canvas, modelConfig, noPlate) {
		var me = this, config = {
			start: [0, 0], end: [0, 0]
		};
		var flow = me.callParent(arguments);
		Ext.apply(flow, config);
		if (!noPlate) {
			var path = flow.attrs.path, len = path.length, flowPlate = flow.clone().
					attr({'arrow-end': '', 'stroke-width': 16, stroke: 'white'}).toBack()
			flow.flowPlate = flowPlate;
			flowPlate.flow = flow;
			flowPlate.start = flow.start = [path[0][1], path[0][2]];
			flowPlate.end = flow.end = [path[len - 1][1], path[len - 1][2]];
			if (!canvas.readOnly) {
				flowPlate.drag(me.onPlateMove, me.onDDStart, me.onPlateEnd);
			}
		}
		return flow;
	},
	onDDStart: function(x, y, e) {

	},
	onDDMove: function(dx, dy, x, y, e) {
		var flow = this, path = [
			['M', flow.start[0] + dx, flow.start[1] + dy],
			['L', flow.end[0] + dx, flow.end[1] + dy]
		];
		flow.attr({path: path});
		if (flow.flowPlate) {
			flow.flowPlate.attr({path: path});
		}
	},
	onDDEnd: function(x, y, e) {
		var flow = this, flowPlate = flow.flowPlate, canvas = flow.canvas, path = flow.attrs.path;
		flow.start = [path[0][1], path[0][2]];
		flow.end = [path[1][1], path[1][2]];
		if (flowPlate) {
			flowPlate.start = flow.start;
			flowPlate.end = flow.end;
		}
		canvas.viewAnchor(canvas, flow);
	},
	onPlateMove: function(dx, dy, x, y, e) {
		var flowPlate = this, flow = flowPlate.flow, path = [
			['M', flow.start[0] + dx, flow.start[1] + dy],
			['L', flow.end[0] + dx, flow.end[1] + dy]
		];
		flow.attr({path: path});
		flow.flowPlate.attr({path: path});
	},
	onPlateEnd: function(x, y, e) {
		var flowPlate = this, flow = flowPlate.flow, canvas = flow.canvas, path = flow.attrs.path;
		flowPlate.start = flow.start = [path[0][1], path[0][2]];
		flowPlate.end = flow.end = [path[1][1], path[1][2]];
		canvas.viewAnchor(canvas, flow);
	}
});